/**
 * Cordova Plugin Manager for OpenHarmony
 * Manages the lifecycle and execution of Cordova plugins
 */
export class CordovaPluginManager {
  private static instance: CordovaPluginManager;
  private plugins: Map<string, any> = new Map();

  private constructor() {}

  public static getInstance(): CordovaPluginManager {
    if (!CordovaPluginManager.instance) {
      CordovaPluginManager.instance = new CordovaPluginManager();
    }
    return CordovaPluginManager.instance;
  }

  /**
   * Register a Cordova plugin
   * @param name Plugin name
   * @param plugin Plugin implementation
   */
  public registerPlugin(name: string, plugin: any): void {
    this.plugins.set(name, plugin);
  }

  /**
   * Get a registered plugin
   * @param name Plugin name
   * @returns Plugin instance or undefined
   */
  public getPlugin(name: string): any {
    return this.plugins.get(name);
  }

  /**
   * Execute a plugin method
   * @param pluginName Plugin name
   * @param methodName Method name
   * @param args Method arguments
   * @returns Promise with result
   */
  public async executePlugin(pluginName: string, methodName: string, args: any[]): Promise<any> {
    const plugin = this.getPlugin(pluginName);
    if (!plugin) {
      throw new Error(`Plugin ${pluginName} not found`);
    }

    if (typeof plugin[methodName] !== 'function') {
      throw new Error(`Method ${methodName} not found in plugin ${pluginName}`);
    }

    return plugin[methodName](...args);
  }
}